﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace DotConferenceSql
{
    /// <summary>
    /// QueryElementBase is the base of all elements that are used
    /// in the SQL query generator. If you want to create a custom
    /// element for the query, inherit this class.
    /// </summary>
    public class QueryElementBase
    {
        private List<SqlParameter> m_parameters = new List<SqlParameter>();

        /// <summary>
        /// Gets the SqlParameter list.
        /// </summary>
        /// <value>The parameters.</value>
        protected List<SqlParameter> Parameters
        {
            get
            {
                return m_parameters;
            }
        }

        private QueryElementType m_type = QueryElementType.Other;
        /// <summary>
        /// Gets or sets the type of clause for this element.
        /// </summary>
        /// <value>The type.</value>
        public QueryElementType Type
        {
            get
            {
                return m_type;
            }
            set
            {
                m_type = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="QueryElementBase"/> class.
        /// </summary>
        public QueryElementBase()
        {
        }

        /// <summary>
        /// Lists the parameters. Custom elements
        /// can reimplement this method to get custom
        /// behavior
        /// </summary>
        /// <returns></returns>
        public virtual List<SqlParameter> ListParameters()
        {
            return Parameters;
        }

        /// <summary>
        /// Generate the part of the query for this element.
        /// </summary>
        /// <returns>generated SQL fragment</returns>
        public virtual string BuildTextQuery()
        {
            return String.Empty;
        }

        /// <summary>
        /// Useful method used to sort the element in
        /// order for the query. Use the order of the items
        /// in the enum
        /// </summary>
        /// <param name="x">First value</param>
        /// <param name="y">Second value</param>
        /// <returns>comparator</returns>
        public static int QueryElementComparator(QueryElementBase x, QueryElementBase y)
        {
            if (x.Type < y.Type)
                return -1;
            else if (x.Type == y.Type)
                return 0;
            else if (x.Type > y.Type)
                return 1;

            return 0;
        }
    }
}
